package com.tarena.lbs.stock.web.repository;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tarena.lbs.pojo.stock.po.CouponStockPO;
import com.tarena.lbs.stock.web.mapper.CouponStockMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
@Slf4j
public class CouponStockRepository {
    @Autowired
    private CouponStockMapper couponStockMapper;
    public int saveCouponStock(CouponStockPO poParam) {
        return couponStockMapper.insert(poParam);
    }


    public Integer getStockByCouponId(Integer couponId) {
        //select * from coupon_stock where coupon_id=#{couponId};
        QueryWrapper<CouponStockPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("coupon_id", couponId);
        CouponStockPO couponStockPO = couponStockMapper.selectOne(queryWrapper);
        return couponStockPO.getNum();
    }

    public Integer reduceStockByCouponId(Integer couponId) {
        //update coupon-stock set num=num-#{1} where couponId=#{couponId} and num>0
        return couponStockMapper.updateNumByCouponId(couponId);
    }
}
